<?php

/**
 * EmsImage form.
 *
 * @package    ems1
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class EmsImageForm extends BaseEmsImageForm
{
    public function configure()
    {
        unset ($this['content_id']);

        $image_sizes = Doctrine::getTable('EmsImageSize')->getEnabledSizes();
        $files = $this->getObject()->getFiles();

        if($files->count()>0){
            foreach ($files as $file){
                foreach ($image_sizes as $key => $size) {
                    if($size->getId() == $file->getSize()->getId()){
                        $image_sizes->remove($key);
                        break;
                    }
                }
            }
        }
        
        foreach ($image_sizes as $i=>$size) {
            $file = new EmsImageFile();
            $file->setSize($size);
            $this->getObject()->getFiles()->add($file);
            $files->add($file);
        }

        $files_forms = new sfForm();
        foreach ($files as $i=>$file){
            $file_form = new EmsImageFileForm($file,array('portal_id' => $this->getOption('portal_id')));
            $files_forms->embedForm($i, $file_form);
        }

        $files_forms->validatorSchema->setPostValidator(new ImageFilesValidatorSchema(array('empty' => 'Debe cargar al menos un archivo.')));

        $this->embedForm('Files', $files_forms);
    }
}